/**  
 * @Title: ComponentRegistryService.java
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-05-08 08:30:27 
 */  
package com.fsxgt.datagrid.flow.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import com.fsxgt.datagrid.flow.exception.FlowException;
import com.fsxgt.datagrid.flow.handler.IComponentHandler;
import com.fsxgt.datagrid.flow.service.IComponentService;

/**  
 * @ClassName: ComponentRegistryService
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-05-08 08:30:27 
*/
@Service
public class CmpRegistryService {

	private Map<String, IComponentHandler> componentHandlerRegistry = null;
	private Map<String, IComponentService> componentServiceRegistry = null;
	@Autowired
	private ApplicationContext applicationContext;
	
	
	@PostConstruct
	private void init() {
		
		Map<String, IComponentHandler> handlerMap = applicationContext.getBeansOfType(IComponentHandler.class);
		
		Map<String, IComponentService> serviceMap = applicationContext.getBeansOfType(IComponentService.class);
		
		componentHandlerRegistry=new HashMap<String, IComponentHandler>();
		componentServiceRegistry=new HashMap<String, IComponentService>();
		
		
		for(IComponentHandler obj:handlerMap.values()) {
			componentHandlerRegistry.put(obj.getComponentType(), obj);
		}
		
		for(IComponentService obj:serviceMap.values()) {
			componentServiceRegistry.put(obj.getComponentType(), obj);
		}
		

	}
	

	
	public IComponentHandler getHandler(String componentType) throws FlowException {
		
		IComponentHandler component = componentHandlerRegistry.get(componentType);
		if(component==null) {
			throw new FlowException("找不到组件类型"+componentType);
		}
		
		return component;
	}
	public IComponentService getService(String componentType) throws FlowException {
		
		IComponentService service = componentServiceRegistry.get(componentType);
		
		if(service==null) {
			throw new FlowException("找不到组件类型"+componentType);
		}
		
		return service;
	}
	
}
